package com.bachelor.community.service.impl;

import com.bachelor.community.mapper.AnswerMapper;
import com.bachelor.community.mapper.CollectionMapper;
import com.bachelor.community.mapper.QuestionMapper;
import com.bachelor.community.mapper.UserMapper;
import com.bachelor.community.model.entity.Answer;
import com.bachelor.community.model.entity.Collection;
import com.bachelor.community.model.entity.Question;
import com.bachelor.community.model.entity.User;
import com.bachelor.community.model.vo.AnswerVO;
import com.bachelor.community.service.intf.CollectionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {
    @Resource
    private CollectionMapper collectionMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private AnswerMapper answerMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public IPage<AnswerVO> listCollectionsByUserId(Integer currentPage, Integer pageSize, Integer userId) {
        IPage<AnswerVO> page = new Page<>(currentPage, pageSize);
        collectionMapper.listCollections(page, userId);
        page.getRecords().forEach(record -> {
            Question question = questionMapper.selectById(record.getQuestionId());
            Answer answer = answerMapper.selectById(record.getId());
            User answerer = userMapper.selectById(answer.getAnswerer());
            record.setTitle(question.getTitle());
            record.setUsername(answerer.getUsername());
        });
        return page;
    }
}
